package com.leetcode.array;

public class Leetcode81 {

    public boolean search(int[] nums, int target) {
        int left = 0;
        int right = nums.length -1;
        while (left <= right) {
            int mid = left + (right - left) /2;
            if (target == nums[mid])
                return true;
            if (nums[left] == nums[mid]) {
                left++;
                continue;
            }
            if (nums[left] < nums[mid]) {
                // 前半边是有序的
                if (target < nums[mid] && target >= nums[left]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                // 后边部分有序
                if (target < nums[right] && target >= nums[mid]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = {1, 3, 5};
        Leetcode81 leet = new Leetcode81();
        leet.search(arr, 1);
    }

}
